package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.springframework.stereotype.Service;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {


    /**
     * 分页查询敏感词
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listSensitive(SensitiveDto dto) {
            //1. 校验并修正数据
            if(null == dto){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
            dto.checkParam(); //修正数据

            //3. 根据分页方法的提示设置分页参数
            IPage<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());

            //4. 根据分页方法的提示设置查询的条件
            LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(WmSensitive::getSensitives, dto.getName());
            //设置排序
            queryWrapper.orderByDesc(WmSensitive::getCreatedTime);

            //2. 调用service的分页方法完成分页的查询
            page(page,queryWrapper);

            //5. 封装并返回结果
            PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(),
                    dto.getSize(),
                    (int)page.getTotal());
            pageResponseResult.setData(page.getRecords());
            return pageResponseResult;
        }

}
